package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 权限控制相关的Controller
 */

@RestController
@RequestMapping("/permission")
public class PermissionController {
    //引用服务提供者的 PermissionService
    @Reference
    private PermissionService permissionService;

    /**
     * 分页查询，条件查询，模糊查询
     * @param queryPageBean
     * @return
     */
    @RequestMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean){

        try {
            //调用service层进行分页查询
            PageResult page = permissionService.findPage(queryPageBean);

            //查询成功返回PageResult对象
            return new Result(true, "查询权限列表成功",page);

        } catch (Exception e) {
            e.printStackTrace();
            //到这里说明查询权限列表失败
            return new Result(false,"查询权限列表失败");
        }
    }

    /**
     * 添加权限
     * @param permission 权限对象
     * @return
     */
    @RequestMapping("/addPermission")
    public Result addPermission(@RequestBody Permission permission){
        try {
            //调用service层的方法，添加权限
            permissionService.addPermission(permission);

            //添加成功
            return new Result(true,"添加权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加权限失败");
        }
    }


    /**
     * 根据id删除权限
     * @param id
     * @return
     */
    @RequestMapping("/delPermission")
    public Result delPermissionById(Integer id){

        System.out.println(id);

        try {
            permissionService.delPermissionById(id);

            return new Result(true,"删除权限成功");

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除权限失败");
        }

    }


    /**
     * 根据id查询权限信息
     * @param id
     * @return
     */
    @RequestMapping("/findPermissionById")
    public Result findPermissionById(Integer id){

        try {
            Permission permission = permissionService.findPermissionById(id);

            return new Result(true,"根据id查询权限信息成功",permission);

        } catch (Exception e) {
            e.printStackTrace();

            return new Result(true,"根据id查询权限信息失败");

        }
    }


    /**
     * 修改权限信息
     * @param permission
     * @return
     */
    @RequestMapping("/editPermissionById")
    public Result editPermissionById(@RequestBody Permission permission){

        try {
            //调用service方法 修改权限信息
            permissionService.editPermissionById(permission);

            return new Result(true,"修改权限成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"修改权限失败");
        }

    }



    //刘威写的


    /**
     * 查询所有权限信息
     * @return
     */
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            //调用业务层查询所有权限信息
            List<Permission> list = permissionService.findAll();
            return new Result(true,"权限信息查询成功",list);//查询成功
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"权限信息查询成功");//查询失败
        }
    }


    /**
     * 添加权限信息
     * @param permission
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission){
        try{
            //调用业务层添加权限信息
            permissionService.add(permission);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"新增权限失败");//新增失败
        }
        return new Result(true,"新增权限成功");//新增成功
    }

}
